<?php

namespace App\Controller\System;

use App\Controller\ApiController;
use Usap\Service\Service;
use Think\Cache;


/**
 *
 * @desc 应用配置
 */
class AlarmsController extends ApiController
{
    /**
     * 获取警报信息
     */
    public function getAlarms()
    {
        $user_id = $this->context->userId;
        \Logger::getLogger('home.user.account')->apps('login_start')->info(I());
        if (empty($user_id)) {
            $this->ajaxReturn(array('code' => -1001, 'msg' => "请先登录"));
        }
        $alarms_service = Service::instance()->find('common', 'alarms');
        $devices_service = Service::instance()->find('devices', 'devices');
        $alarmsTypeconfig = Service::instance()->loadConfig('common', 'common', 'alarmsTypeConfig');
        $platformConfig = Service::instance()->loadConfig('common', 'common', 'platformConfig');
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        $admin_db = D('Usap/Admin/Admin');
        $request = I('request.');
        $page = $request['page'] ? $request['page'] : 1;
        $rows = $request['rows'] ? $request['rows'] : 20;
        $order = $request['order'] ? $request['order'] : 'a.alarm_id';
        $sort = $request['sort'] ? $request['sort'] : 'desc';
        $state = $request['state'] ? $request['state'] : 'on';
        $where = 1;
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];
            $where .= " and a.codeserial like '%$keyword%' or b.device_address like '%$keyword%'";
        }
        if ($state == 'on') {
            $status = 0;
        } else {
            $status = 1;
        }
        $where .= " and a.status = '$status'";
        $alarms_list = $alarms_service->getAlarmsList($where, $page, $rows, $order, $sort, $files = '*');
        $list = [];
        if ($alarms_list['count'] > 0) {
            $list['count'] = $alarms_list['count'];
            foreach ($alarms_list['list'] as $value) {
                $res['id'] = $value['alarm_id'];
                $device_info = $devices_service->getInfoByCodeserial($value['codeserial']);
                $user_info = $admin_db->where(['userid' => $value['user_id']])->find();
                $res['user'] = $value['user_id'] ? $user_info['username'] : '';
                $res['category'] = $device_categroy_config[$device_info['device_category']];//设备类型
                $res['device_address'] = $device_info['device_address'];//设备地址
                $res['codeserial'] = $value['codeserial'];//设备编号
                $res['status'] = $value['status'];//警报状态
                $res['type'] = $alarmsTypeconfig[$value['type']];//警报类型
                $res['remark'] = $value['remark'];//处置结果
                $res['platform'] = $value['disposal_platform'] ? $platformConfig[$value['disposal_platform']] : '';
                $res['created_time'] = date('Y-m-d H:i:s', $value['created_time']);
                $res['dealed_time'] = date('Y-m-d H:i:s', $value['updated_time']);
                $list['alarms_list'][] = $res;
            }
        }
        $return = array('code' => '0000', 'msg' => '获取数据成功', 'data' => $list);
        $this->ajaxReturn($return);
    }


    /**
     * 警报解除
     */
    public function deal_alarms()
    {
        $user_id = $this->context->userId;
        \Logger::getLogger('home.user.account')->apps('login_start')->info(I());
        if (empty($user_id)) {
            $this->ajaxReturn(array('code' => -1001, 'code_str' => "请先登录"));
        }
        $alarms_service = Service::instance()->find('common', 'alarms');
        $devices_service = Service::instance()->find('devices', 'devices');
        $request = I('request.');
        $id = $request['id'];
        $remark = $request['remark'];
        $platform = $request['platform'];
        //查询对应巡检任务
        $alarms_info = $alarms_service->getAlarmsInfoById($id);
        if (!$alarms_info) {
            $this->ajaxReturn(array('code' => -404, 'msg' => "警报信息错误", 'data' => ['info' => []]));
        }
        $data = [
            'status' => 1,
            'user_id' => $user_id,
            'remark' => $remark,
            'disposal_platform' => $platform,
            'updated_time' => time(),
        ];
        $codeserial = $alarms_info['codeserial'];
        $res = $alarms_service->updateAlarmsInfo($id, $data);
        $devices_service->updateDeviceBycodeserial($codeserial, array('is_alarming' => 0));
        if ($res) {
            $return = array('code' => '0000', 'msg' => '解除成功', 'data' => ['info' => []]);
            $this->ajaxReturn($return);
        } else {
            $return = array('code' => '-10001', 'msg' => '因系统异常，添加失败', 'data' => ['info' => []]);
            $this->ajaxReturn($return);
        }
    }


}
